package com.example.easypan.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.easypan.common.Result;
import com.example.easypan.pojo.FileInfo;
import com.example.easypan.pojo.Folder;
import com.example.easypan.service.impl.FileInfoServiceImpl;
import com.example.easypan.service.impl.FolderServiceImpl;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 功能：
 * 作者：cyan
 * 日期：2025/2/11 下午1:48
 */
@RestController
@RequestMapping("/folder")
public class FolderController {
    @Resource
    private FolderServiceImpl folderService;

    @Resource
    private FileInfoServiceImpl fileInfoService;

    @GetMapping("/list")
    public Result list()
    {
        List<Folder> list = folderService.list();

        return Result.success(list);
    }

    @PostMapping("/add")
    public Result addFolder(@RequestBody Folder folder) {
        String findName = folder.getFolderName();

        // 使用Lambda查询方式查找文件夹是否已存在
        LambdaQueryWrapper<Folder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Folder::getFolderName, findName);

        List<Folder> list = folderService.list(queryWrapper);
        if (list.size() > 0) {
            return Result.error("已经存在该文件夹");
        }

        // 在服务层进行插入操作
        Folder createdFolder = createFolder(folder);
        insertFileInfo(createdFolder);

        return Result.success(createdFolder);
    }

    // 创建文件夹方法，封装事务
    @Transactional
    public Folder createFolder(Folder folder) {
        // 插入文件夹数据库
        folder.setCreateTime(new Date());
        folder.setCreatedBy("example");
        folderService.save(folder);

        // 获取刚刚插入的文件夹
        LambdaQueryWrapper<Folder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Folder::getFolderName, folder.getFolderName());
        return folderService.getBaseMapper().selectOne(queryWrapper);
    }

    // 插入文件信息的方法
    @Transactional
    public void insertFileInfo(Folder folder) {
        // 创建并保存文件信息
        FileInfo fileInfo = new FileInfo();
        fileInfo.setFileName(folder.getFolderName());
        fileInfo.setFolderId(folder.getFolderId());
        fileInfo.setFolderPid(folder.getParentFolderId());
        fileInfo.setFolderType(1); // 文件夹类型
        fileInfoService.save(fileInfo);
    }


//    @PostMapping("/add")
//    public Result addFolder(@RequestBody Folder folder)
//    {
//        String findName=folder.getFolderName();
//
//        LambdaQueryWrapper<Folder> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(Folder::getFolderName,findName);
//
//        List<Folder> list = folderService.list(queryWrapper);
//        if(list.size()>1)
//        {
//            return Result.error("已经存在该文件夹");
//        }
//
//        //获得刚刚创建的路径id
//        Folder dbFolder=folderService.getBaseMapper().selectOne(queryWrapper);
//
//
//        //插入路径数据库
//        folder.setCreateTime(new Date());
//        folder.setCreatedBy("example");
//        folderService.save(folder);
//
//
//        //文件夹插入文件数据库
//        FileInfo fileInfo=new FileInfo();
//        fileInfo.setFileName(folder.getFolderName());
//        //设置文件夹自己的路径
//        fileInfo.setFolderId(dbFolder.getFolderId());
//        //设置文件夹的父路径
//        fileInfo.setFolderPid(folder.getParentFolderId());
//        fileInfo.setFolderType(1);
//        fileInfoService.save(fileInfo);
//
//
//        return Result.success(folder);
//    }



}